﻿package Welsy4.Connect.commands
{
	import com.adobe.cairngorm.commands.SequenceCommand;
	import flash.net.URLVariables;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.IResponder;
	import mx.controls.Alert;
	
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import Welsy4.Connect.business.MeetingDelegate;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.Meeting;
	
	
	
	public class CreatePublicMeetingCommand extends SequenceCommand implements Command, IResponder
	{
		
		public function CreatePublicMeetingCommand() 
		{
			
		}
		override public function execute(event:CairngormEvent):void {
			/*var createUserDelegate : CreateUserDelegate = new CreateUserDelegate(this);
			var adobeEvent : AdobeEvent = AdobeEvent (event);
			createUserDelegate.CreateNewUser(adobeEvent.user);
			*/
			ConnectModel.getInstance().addQueue("CreatePublicMeetingCommand.execute");
			
			this.nextEvent = ChainEvent(event).nextChainedEvent;
						
			var createMeetingRequest:URLVariables =  ChainEvent(event).eventRequest;
			
			var meetingDelegate:MeetingDelegate = new MeetingDelegate(this);
			meetingDelegate.createMeeting(createMeetingRequest.name, ConnectModel.getInstance().SCOShortcutRequested.sco_id, createMeetingRequest.date_begin, createMeetingRequest.date_end, createMeetingRequest.url_path);
		    
			//Alert.show("Request :\n" + createMeetingRequest.toString());
		}
		
		public function result(event:Object):void {
		    ConnectModel.getInstance().addQueue("CreatePublicMeetingCommand.result ");
			
			ConnectModel.getInstance().httpServiceHandler = new HttpServiceHandler();
			ConnectModel.getInstance().httpServiceHandler.setXML(event.result);
			
			var meetingCreated:Meeting = ConnectModel.getInstance().httpServiceHandler.getMeetingJustCreated();
			
			if (meetingCreated != null) {
				ConnectModel.getInstance().lastMeetingCreated = meetingCreated;
				/* CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.MEETING_CREATED, true, ResponseEvent.OK, ConnectModel.getInstance().httpServiceHandler.xml)); */
				this.executeNextCommand();
			} else {
				CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.CREATE_MEETING_COMMAND_FAULT, ConnectModel.getInstance().httpServiceHandler.xml));
			}
		}
		
		public function fault(event:Object):void {
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.CONNECTION));
			ConnectModel.getInstance().addQueue("CreatePublicMeetingCommand.fault");
		}
	}
	
}